package com.iiapk.gridnew;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.iiapk.core.widget.FlingGallery;
import com.iiapk.grid.database.ArticleManager;
import com.iiapk.gridnew.domain.Article;
import com.iiapk.gridnew.view.NewsPanel;
import com.iiapk.gridnew.view.NewsPanel1;

public class FlingGalleryActivity extends Activity {
	
	List<Article> dataSource = new ArrayList<Article>();
	private FlingGallery mGallery;
	private ImageView refreshBT;
	private RefreshTask refreshTask;

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return mGallery.onGalleryTouchEvent(event);
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gallery);
		String columnId = this.getIntent().getStringExtra("id");
		//String title = this.getIntent().getStringExtra("title");
		//TextView textView = (TextView)findViewById(R.id.panel_header_text);
		//textView.setText(title);
		findViewById(R.id.panel_header_homeBT).setOnClickListener(
				new OnClickListener(){
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(FlingGalleryActivity.this,
						GridPagerActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.hold,R.anim.push_right_out);
			}
		});
		
		refreshBT = (ImageView)findViewById(R.id.panel_header_refreshBT);
		refreshBT.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				beginRefreshTask();
			}
		});
		
		ArticleManager articleManager = new ArticleManager(this);
		dataSource = articleManager.select(columnId);
		
		dataSource.get(0).imgR=R.drawable.test1;
		dataSource.get(1).imgR=R.drawable.test2;
		dataSource.get(2).imgR=R.drawable.test3;
		dataSource.get(3).imgR=R.drawable.test4;
		dataSource.get(4).imgR=R.drawable.test5;
		dataSource.get(5).imgR=R.drawable.test6;
		dataSource.get(6).imgR=R.drawable.test7;
		
		mGallery = (FlingGallery)findViewById(R.id.gallery_fling);
		mGallery.setAnimationDuration(500);
		mGallery.setIsGalleryCircular(false);
		mGallery.setAdapter(new FlingGalleryAdapter(this));
	}
	
	private void beginRefreshTask() {
		if (refreshTask != null) {
			refreshTask.cancel(true);
		}
		refreshTask = new RefreshTask();
		refreshTask.execute();
	}
	
	public class FlingGalleryAdapter extends BaseAdapter {
		
		private Context mContext;
		
		public FlingGalleryAdapter(Context context) {
			super();
			mContext=context;
		}
		public int getCount() {
			return dataSource.size();
		}
		public Object getItem(int position) {
			return position;
		}
		public long getItemId(int position) {
			return position;
		}
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = null;
			if(convertView instanceof NewsPanel1){
				((NewsPanel1)convertView).refreshDataSource();
				view=convertView;
			}else{
				view=new NewsPanel1(mContext,dataSource);
			}
			return view;
		}
	}
	
	class RefreshTask extends AsyncTask<String, Void, Void> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			refreshBT.setBackgroundResource(R.anim.main_loading);
			AnimationDrawable frameAnimation=(AnimationDrawable)refreshBT.getBackground(); 
			frameAnimation.start(); 
		}
		@Override
		protected Void doInBackground(String... params) {
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return null;
		}
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			refreshBT.setBackgroundResource(R.drawable.tab_refresh_selector);
		}
	}

	public List<Article> getDataSource() {
		return dataSource;
	}
	
}
